<?php

$task = JRequest::getVar('task', null);

if($task)
	Je_Comment::$task();
	

class Je_Comment
{
	function display($itemId, $component, $title = '', $only = null)
	{
		echo '<link rel="stylesheet" href="'.JURI::base().'components/com_je_comment/html/css/je_comment.css" type="text/css" />';
		echo '<script type="text/javascript" src="'.JURI::base().'components/com_je_comment/html/js/1.5.1/jquery.min.js"></script>';
		
		//require comment
		require_once JPATH_SITE . DS . 'components' . DS . 'com_je_comment' . DS . 'models' . DS . 'comments.php';
		
		//require language
		//changed by anpv :D
		//require_once JPATH_SITE . DS . 'components' . DS . 'com_je_comment' . DS . 'languages' . DS . 'en.php';
		$lang = & JFactory::getLanguage();
		$lang->load('com_je_comment', JPATH_BASE, null, false);
		// end - anpv
		
		$path = $comments = JPATH_SITE . DS . 'components' . DS . 'com_je_comment' . DS . 'views' . DS;
		
		//get data
		$listComments = ModelComments::getList($itemId, $component);
		
		$numberOfComments = count($listComments);
		
		if($only == 'form')
			require_once $path . 'comment_form.php';
		elseif($only == 'list')
			require_once $path . 'comment_list.php';
		else
		{			
			
			require_once $path . 'comment_form.php';
			require_once $path . 'comment_list.php';
		}
		
		return true;
	}
	
	function save()
	{
		//require model file
		require_once JPATH_SITE . DS . 'components' . DS . 'com_je_comment' . DS . 'models' . DS . 'comments.php';
		
		$post = JRequest::get('post');
		$session = JFactory::getSession();
		
		$user = JFactory::getUser();
		
		require_once(JPATH_ROOT . DS . 'jelibs' . DS . 'classes' . DS . 'recaptchalib.php');

		// Get a key from https://www.google.com/recaptcha/admin/create
		$privatekey = CFG_RECAPTCHA_PRIVATE_KEY;
		
		# the response from reCAPTCHA
		$resp = null;
		
		$server = JRequest::get('server');
		
		if($post['captcha'] && $user->guest)
		{
			$resp = recaptcha_check_answer (	$privatekey,
												$server["REMOTE_ADDR"],
												$post['recaptcha_challenge_field'],
												$post['captcha']);
		            
			if (!$resp->is_valid)
			{
				echo 'INVALID';
				exit();
			}
		}
		
		if(ModelComments::save())
			echo 'OK';
		else 
			echo 'ERROR';
		
		exit();
	}
	
	function refresh()
	{
		$itemId = JRequest::getVar('item_id');
		$component = JRequest::getVar('component');
		$countComment = JRequest::getVar('count_comment');
		
		require_once JPATH_SITE . DS . 'components' . DS . 'com_je_comment' . DS . 'models' . DS . 'comments.php';
		
		//get data
		$listComments = ModelComments::getList($itemId, $component);
		
		$path = $comments = JPATH_SITE . DS . 'components' . DS . 'com_je_comment' . DS . 'views' . DS;
		
		if($countComment)
			echo ModelComments::countComments($itemId, $component);
		else
			require_once $path . 'comment_item.php';
		
		exit();
	}
	
	function get_image()
	{
		require_once JPATH_COMPONENT_SITE . DS . 'helpers' . DS . 'kcaptcha' . DS . 'kcaptcha.php';
		
		$session = JFactory::getSession();
		$session->set('kcaptcha', null);
		
		$captcha = new KCAPTCHA();
		
		if(JRequest::get(session_name()))
		{
			$session->set('je_kcaptcha', $captcha->getKeyString());
		}
		
		exit();
	}
}